package com.sokal.easycare.modules.service.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.service.CrudService;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.modules.service.entity.SerService;
import com.sokal.easycare.modules.service.dao.SerServiceDao;
import com.sokal.easycare.modules.service.entity.SerAction;
import com.sokal.easycare.modules.service.dao.SerActionDao;

/**
 * 服务Service
 * @author liliangdong
 * @version 2017-03-27
 */
@Service
@Transactional(readOnly = true)
public class SerServiceService extends CrudService<SerServiceDao, SerService> {

	@Autowired
	private SerActionDao serActionDao;
	
	public SerService get(String id) {
		SerService serService = super.get(id);
		serService.setSerActionList(serActionDao.findList(new SerAction(serService)));
		return serService;
	}
	
	public List<SerService> findList(SerService serService) {
		return super.findList(serService);
	}
	
	public Page<SerService> findPage(Page<SerService> page, SerService serService) {
		return super.findPage(page, serService);
	}
	
	@Transactional(readOnly = false)
	public void save(SerService serService) {
		super.save(serService);
		for (SerAction serAction : serService.getSerActionList()){
			if (serAction.getId() == null){
				continue;
			}
			if (SerAction.DEL_FLAG_NORMAL.equals(serAction.getDelFlag())){
				if (StringUtils.isBlank(serAction.getId())){
					serAction.setSerService(serService);
					serAction.preInsert();
					serActionDao.insert(serAction);
				}else{
					serAction.preUpdate();
					serActionDao.update(serAction);
				}
			}else{
				serActionDao.delete(serAction);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(SerService serService) {
		super.delete(serService);
		serActionDao.delete(new SerAction(serService));
	}
	
}